package redisLearn;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class RedisClient {

	// *<number of arguments> CR LF
	// $<number of bytes of argument 1> CR LF
	// <argument data> CR LF
	// ...
	// $<number of bytes of argument N> CR LF
	// <argument data> CR LF

	Socket conn;
	public RedisClient() throws IOException {
		conn = new Socket("localhost", 6379);
	}

	// 模拟 set操作
	public  void set(String key, String value) throws IOException {
		OutputStream os = conn.getOutputStream();
		StringBuilder sb = new StringBuilder();

		sb.append("*").append(3).append("\r\n");
		
		sb.append("$").append("set".getBytes().length).append("\r\n");
		sb.append("set").append("\r\n");

		sb.append("$").append(key.getBytes().length).append("\r\n");
		sb.append(key).append("\r\n");

		sb.append("$").append(value.getBytes().length).append("\r\n");
		sb.append(value).append("\r\n");

		os.write(sb.toString().getBytes());
		
		byte[] result = new byte[1024];
		conn.getInputStream().read(result);
		System.out.println(new String(result));

	}

	public static void main(String[] args) throws IOException {
		RedisClient client = new RedisClient();
		client.set("fuck", "you");
	}

}
